package com.ezjie.ielts.db;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.model.EwordQuestion;
import com.ezjie.ielts.util.DownloadUtil;
import com.ezjie.ielts.util.LogUtils;

public class WordQuestionDao {

	private static final String TABLE_NAME = "e_word_question";
	private static final String COLUMN_QUESTION_ID = "question_id";
	private static final String COLUMN_WORD_ID = "word_id";
	private static final String COLUMN_TYPE = "question_type";
	private static final String COLUMN_QUESTIONS = "questions";
	private static final String COLUMN_CREATE_TIME = "create_time";
	private static final String COLUMN_UPDATE_TIME = "update_time";
	private SQLiteDatabase database;

	public WordQuestionDao(Context context) {
		DownloadUtil.initCacheDir(context);
		String databaseFilename = DownloadUtil.WORD_CACHE_PATH + "/" + DownloadUtil.WORD_DATABASE_FILENAME;
		File file = new File(databaseFilename);
		if (!file.exists()) {
			// 如果数据库文件不存在 就再拷贝一份
			DownloadUtil.getInstance().copyWordDB2SD(context);
		}

		if (file.exists()) {
			database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		}
	}

	/**
	 * 根据单词id 查找单词
	 * 
	 * @param word_id
	 * @return
	 */
	public List<EwordQuestion> findByWordId(int word_id) {
		List<EwordQuestion> mQuestions = new ArrayList<EwordQuestion>();
		if (database == null) {
			return mQuestions;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_WORD_ID + "=?", new String[] { word_id + "" }, null,
				null, null);
		while (cursor.moveToNext()) {
			EwordQuestion ewordQuestion = new EwordQuestion();
			ewordQuestion.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordQuestion.setQuestion_type(cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)));
			ewordQuestion.setQuestion_id(cursor.getInt(cursor.getColumnIndex(COLUMN_QUESTION_ID)));
			ewordQuestion.setQuestions(cursor.getString(cursor.getColumnIndex(COLUMN_QUESTIONS)));
			ewordQuestion.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordQuestion.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));
			mQuestions.add(ewordQuestion);
		}
		cursor.close();
		return mQuestions;
	}

	public EwordQuestion findByQuestionId(int question_id) {
		EwordQuestion ewordQuestion = new EwordQuestion();
		if (database == null) {
			return ewordQuestion;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_QUESTION_ID + "=?", new String[] { question_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordQuestion.setQuestion_id(cursor.getInt(cursor.getColumnIndex(COLUMN_QUESTION_ID)));
			ewordQuestion.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordQuestion.setQuestion_type(cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)));
			ewordQuestion.setQuestions(cursor.getString(cursor.getColumnIndex(COLUMN_QUESTIONS)));
			ewordQuestion.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordQuestion.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));

		}
		cursor.close();
		return ewordQuestion;
	}

	public EwordQuestion findByQuestionIdOrNull(int question_id) {
		EwordQuestion ewordQuestion = null;
		if (database == null) {
			return ewordQuestion;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_QUESTION_ID + "=?", new String[] { question_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordQuestion = new EwordQuestion();
			ewordQuestion.setQuestion_id(cursor.getInt(cursor.getColumnIndex(COLUMN_QUESTION_ID)));
			ewordQuestion.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordQuestion.setQuestion_type(cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)));
			ewordQuestion.setQuestions(cursor.getString(cursor.getColumnIndex(COLUMN_QUESTIONS)));
			ewordQuestion.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordQuestion.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));

		}
		cursor.close();
		return ewordQuestion;
	}

	public void insert(EwordQuestion eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();

			values.put(COLUMN_QUESTION_ID, eword.getQuestion_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_TYPE, eword.getQuestion_type());
			values.put(COLUMN_QUESTIONS, eword.getQuestions());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());

			long insert = database.insert(TABLE_NAME, null, values);
			if (insert != -1) {
				LogUtils.d("插入wordQuestion成功");
			} else {
				LogUtils.d("插入wordQuestion失败");
			}
		}
	}

	public void update(EwordQuestion eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();

			values.put(COLUMN_QUESTION_ID, eword.getQuestion_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_TYPE, eword.getQuestion_type());
			values.put(COLUMN_QUESTIONS, eword.getQuestions());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());

			int update = database.update(TABLE_NAME, values, COLUMN_QUESTION_ID + "=?",
					new String[] { eword.getQuestion_id() + "" });
		}
	}

	public void updateList(List<EwordQuestion> list) {
		for (EwordQuestion ewordQuestion : list) {
			EwordQuestion findByQuestionId = findByQuestionIdOrNull(ewordQuestion.getQuestion_id());
			if (findByQuestionId == null) {
				insert(ewordQuestion);
			} else {
				update(ewordQuestion);
			}
		}
	}
}
